var JS_FILES=[];
Function.prototype.bind=function(obj){
	var t=this;
	return function(){
		return t.apply(obj,arguments);
	}
};
Array.prototype.isIn=function(p){
	var l=this.length;
	for(var i=l-1;i>=0;i--)
		if(p==this[i])	return i;
	return -1;
};
String.prototype.isJson=function(){
	var str=this.replace(/\\./g,'@').replace(/"[^"\\\n\r]*"/g,'');
	return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str);
};
function debug(str){
	$('debug').appendChild($C("span",{},' '+str));
}
function $C(eName,propertys){
	var childObj,argLen=arguments.length;
	var domAttr;
	var tmpObj=parent.document.createElement(eName);
	var fix={'class':'className','colspan':'colSpan','rowspan':'rowSpan'};
	for(var pro in propertys){
		domAttr=fix[pro]||pro;
		tmpObj[domAttr]=propertys[pro];
	}
	if(argLen==2)return tmpObj;
	for(var i=2;i<argLen;i++){
		childObj=arguments[i];
		if('string'==typeof(arguments[i]))
		tmpObj.innerHTML+=arguments[i];
		else{
			try{
				tmpObj.appendChild(childObj);
			}catch(e){
				if('number'==typeof(arguments[i]))
					tmpObj.innerHTML=arguments[i]+'';
				else
					alert('创建'+eName+'参数中参数:'+arguments[i]+'有问题');
			}
		}
	}
	return tmpObj;
}
function $(id){
	return document.getElementById(id);
}
if (!window.XMLHttpRequest)
var XMLHttpRequest=function(){
	return new ActiveXObject('Microsoft.XMLHTTP');
};
var ajax=new Object();//ajax类
ajax.get=function(url,callback,asynType){
	asynType=(false==asynType)?false:true;
	(new request(url,callback,null,asynType)).doGet();
}
ajax.post=function(url,data,callback){
	(new request(url,callback,data,true)).doPost();
}
function request(url,callback,data,asynType){
	this.url=url;
	this.callback=callback;
	this.data=data;
	this.asynType=asynType;
	this.xmlRequest=new XMLHttpRequest();
}
request.prototype={
	init:function(){
		if(this.asynType)	this.xmlRequest.onreadystatechange=this.checkStatus.bind(this);
	},
	doGet:function(){
		this.init();
		this.url+=(this.url.indexOf('?')>0?'&':'?')+'r='+Math.random();
		this.xmlRequest.open('GET',this.url,this.asynType);
		this.xmlRequest.send(null);
		if(false==this.asynType){
			if(this.isSuccess()){
				return this.callback(this.xmlRequest.responseText);
			}
		}
	},
	doPost:function(){//执行post方法
		this.init();
		this.xmlRequest.open('POST',this.url,this.asynType);
		this.xmlRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		this.xmlRequest.send(this.data);
	},
	checkStatus:function(){
		if(4==this.xmlRequest.readyState){
			if(true==this.isSuccess())
				this.callback(this.xmlRequest.responseText);
			else
				debug('url:'+this.url+' data:'+this.data+' errorCode:'+this.xmlRequest.status+'msg:'+this.xmlRequest.responseText);
		}
	},
	isSuccess:function(){
		return this.xmlRequest.status<300;
	}
};
var eve={//事件绑定与解绑定类
	guid:1,
	observe:function(elem,type,handle,obj){
		if(!handle.guid)	handle.guid=this.guid++;
		var fn=handle;
		handle=function(){
			return fn.apply(obj||this,arguments);
		};
		handle.guid=fn.guid;
		elem[type]={};
		elem[type][fn.guid]=handle;
		if(browser.ie)
			elem.attachEvent("on"+type,handle);
		else
			elem.addEventListener(type,handle,false);
		elem=null;
	},
	stopObserve:function(elem,type,handle){
		if(browser.ie)
			elem.detachEvent("on"+type,elem[type][handle.guid]);
		else
			elem.removeEventListener(type,elem[type][handle.guid],false);
		delete(elem[type][handle.guid]);
		elem=null;
	}
};
var browser=(function(){//浏览器判断
	var b=navigator.userAgent.toLowerCase();
	return {
	safari:/webkit/.test(b),
	opera:/opera/.test(b),
	ie:/msie/.test(b) && !/opera/.test(b),
	mozilla:/mozilla/.test(b) && !/(compatible|webkit)/.test(b)
};})();
function removeDom(obj){//removeChild 不知道是否有用
	var m=obj.childNodes;
	for(var i=m.length-1;i>=0;i--){
		if(m[i].nodeType!=1)	continue;
		if(m[i].childNodes.length>0)
			removeDom(m[i]);
		else{
			try{
				obj.removeChild(m[i]);
				m[i]=null;
			}catch(e){}
		}
	}
	try{
		obj.parentNode.removeChild(obj);
	}catch(e){debug(obj.className);}
	obj=null;
}
function emptyFunc(){}//空函数
function switchEffect(elem,cname){//两事件间效果切换
    (new ourMouseover(elem,cname)).init();
}
function ourMouseover(elem,cname){
    this.element=elem;
    this.cname=cname;
    isOver=false;
	elem=null;
}
ourMouseover.prototype={
    init:function(){
        eve.observe(this.element,'mouseover',this.mouseOver,this);
        eve.observe(this.element,'mouseout',this.mouseOut,this);
    },
    mouseOver:function(){
        if(true==this.isOver)    return;
        addClass(this.element,this.cname);
        this.isOver=true;
    },
    mouseOut:function(){
        removeClass(this.element,this.cname);
        this.isOver=false;
    }
};
function addClass(elem,cName){//添加一个class属性值
	if(elem.className.indexOf(cName)>=0)	return;
	var s=(elem.className=='')?'':' ';
	elem.className+=s+cName;
	elem=null;
}
function removeClass(elem,cName){//去掉一个class值
	var t=elem.className.indexOf(cName);
	if(cName==elem.className)	return elem.className='';
	var s='';
	if(-1==t)	return;
	if(0==t)
		s=cName+' ';
	else
		s=' '+cName;
	elem.className=elem.className.replace(s,'');
	elem=null;
}
function getPosition(e){//取得对象的绝对位置
	var t=e.offsetTop;
	var l=e.offsetLeft;
	while(e=e.offsetParent){
		t+=e.offsetTop;
		l+=e.offsetLeft;
	}
	return [t,l];
}
function getElementsByClass(obj,elem,cname){//按class按节点
	var t=obj.getElementsByTagName(elem);
	var temp=[];
	for(var i=t.length-1;i>=0;i--){
		if(t[i].className!=cname)	continue;
		temp.push(t[i]);
	}
	obj=null;
	return temp;
}
function jsFileLoader(jsFile,callBack){//JS文件动态加载
	this.jsFile=jsFile;
	this.callBack=callBack;
}
jsFileLoader.prototype={
	init:function(){
		var script=$C('script',{'src':'js/'+this.jsFile+'.js','type':'text/javascript'});
		script.defer=true;
		document.getElementsByTagName('head')[0].appendChild(script);
		setTimeout(this.checkIsLoad.bind(this),500);
	},
	checkIsLoad:function(){
		if(-1!=JS_FILES.isIn(this.jsFile)){
			this.callBack();
		}else{
			setTimeout(this.checkIsLoad.bind(this),1000);
		}
	}
};
function initChat(chatTo){//初始化聊天窗口
	if(-1==JS_FILES.isIn('chat')){
		(new jsFileLoader('chat',function(){makeChatWindow(chatTo);})).init();
	}else{
		makeChatWindow(chatTo);
	}
}
function demoLogin(userid){
	ajax.get('checkchatuser.php?action=demologin&userid='+userid,emptyFunc);
}
function makeButton(v){//动态生成按钮
	var b=$C('input',{'type':'button','class':'btn','value':v});
	switchEffect(b,'btn_over');
	return b;
}
function parseJson(jsonString){//解析服务器传回来的Json串.
	return JSON.parse(jsonString);
}
function postParam(jsonObj) {//Ajax Post的时候相当于数据打包
	return 'json='+JSON.stringify(jsonObj);
}